const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const moment = require('moment')
const SECRET = 'asdfta219*&%^jWJSHFJ'


router.post('/updateStation', async (req, res) => {
    //token
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select departmentId from user_role where username = ?'
    db.base(sql, result.username, response_temp => {
        console.log(req.body)
        if (response_temp != null && response_temp.length == 1) {
            sql = 'select workNumber from department_employees WHERE departmentId = ?'
            db.base(sql, response_temp[0].departmentId, response_temp => {
                console.log(response_temp)
                flag = false
                for (let i = 0; i < response_temp.length; i++) {
                    if (req.body.workNumber == response_temp[i].workNumber) {
                        flag = true
                    }
                }
                console.log(flag)
                //为true 说明员工是本部门员工
                if (flag) {
                    //获取员工当前岗位id
                    sql = 'select station_id from employees where work_number = ?'
                    db.base(sql, req.body.workNumber, response_temp => {
                        console.log(response_temp)
                        if (response_temp != null && response_temp.length == 1) {
                            //获取当前岗位信息和目标岗位信息
                            sql = 'select department_name,station_name,region,station_id from departmentInfo where station_id = ? or station_id = ?'
                            //目标岗位
                            temp = String(req.body.des_station).split('-').pop()
                            console.log(temp)
                            db.base(sql, [response_temp[0].station_id, temp], response_temp => {
                                console.log(response_temp)
                                if (response_temp != null && response_temp.length == 2) {
                                    //目标岗位
                                    temmm1 = String(response_temp[0].station_id)
                                    //原岗位
                                    temmm2 = String(response_temp[1].station_id)
                                    if (temp == response_temp[0].station_id) {
                                        temmm1 = response_temp[0].station_name + '-' + response_temp[0].department_name + '(' + response_temp[0].region + ')'
                                        temmm2 = response_temp[1].station_name + '-' + response_temp[1].department_name + '(' + response_temp[1].region + ')'
                                    } else {
                                        temmm2 = response_temp[0].station_name + '-' + response_temp[0].department_name + '(' + response_temp[0].region + ')'
                                        temmm1 = response_temp[1].station_name + '-' + response_temp[1].department_name + '(' + response_temp[1].region + ')'
                                    }
                                    console.log(temmm1)
                                    console.log(temmm2)
                                    sql = 'UPDATE employees SET station_id = ? where work_number = ?'
                                    let data = [
                                        temp,
                                        req.body.workNumber
                                    ]
                                    db.base(sql, data, response_temp => {
                                        console.log(response_temp)
                                        if (response_temp != null && response_temp.affectedRows == 1) {
                                            //获取当前时间
                                            var time = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
                                            sql = 'INSERT into station_change (work_number,change_type,reason,occur_time,origin_station,des_station) VALUES(?,?,?,?,?,?)'
                                            data = [
                                                req.body.workNumber,
                                                req.body.changeType,
                                                req.body.reason,
                                                time,
                                                temmm2,
                                                temmm1
                                            ]
                                            console.log(data)
                                            db.base(sql, data, response_temp => {
                                                console.log(response_temp)
                                                if (response_temp != null && response_temp.affectedRows == 1) {
                                                    res.json({
                                                        result: "success"
                                                    })
                                                }else{
                                                    res.json({
                                                        result:'error'
                                                    })
                                                }
                                            })
                                        }else{
                                            res.json({
                                                result:'error'
                                            })
                                        }

                                    })
                                }else{
                                    res.json({
                                        result:'error'
                                    })
                                }
                            })
                        }
                    })
                }else{
                    res.json({
                        result:'error'
                    })
                }
            })

        }else{
            res.json({
                result:'error'
            })
        }
    })
})
module.exports = router
